package com.xiaolanshu.controller;

import com.xiaolanshu.entity.Collection;
import com.xiaolanshu.entity.Note;
import com.xiaolanshu.entity.Praise;
import com.xiaolanshu.entity.User;
import com.xiaolanshu.interceptor.UserHolder;
import com.xiaolanshu.service.CollectionService;
import com.xiaolanshu.service.NoteService;
import com.xiaolanshu.service.PraiseService;
import com.xiaolanshu.service.UserService;
import com.xiaolanshu.vo.CollectionVo;
import com.xiaolanshu.vo.PraiseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    private UserService userService;
    @Autowired
    private PraiseService praiseService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private NoteService noteService;

    /**
     *获取用户个人资料
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(){
        //从线程当中获取用户id（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();

        User user = userService.findUser(userId);

        return ResponseEntity.ok(user);

    }

    /**
     * 更新用户头像
     * @param headphoto
     * @return
     */
    @PostMapping("/headphoto")
    public ResponseEntity uploadHeadphoto(MultipartFile headphoto) throws Exception {

        String headphotoUrl = userService.uploadHeadphoto(headphoto);

        return ResponseEntity.ok(headphotoUrl);
    }

    /**
     * 更新个人资料
     * @param param
     * @return
     */
    @PutMapping("/update")
    public ResponseEntity updateUserInfo(@RequestBody Map param){

        userService.updateUserInfo(param);
        return ResponseEntity.status(200).body("更新成功");
    }

    /**
     * 个人点赞笔记
     * @return
     */
    @GetMapping("/praise")
    public ResponseEntity userPraiseInfo(){
        //从线程当中获取用户id（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();

        //根据用户id查询点赞笔记列表
        List<Praise> praiseList = praiseService.findUserPraise(userId);
        //遍历praiseList获取noteId，并根据noteId查询笔记信息
        //接收查询出来的笔记信息
        List<PraiseVo> praiseVoList = new ArrayList<>();
        for (Praise p:praiseList
             ) {
            Long noteId = p.getNoteId();
            Note note = noteService.findNoteById(noteId);
            User user = userService.findUser(note.getId());

            PraiseVo praiseVo = new PraiseVo();
            praiseVo.setNote(note);
            praiseVo.setUser(user);

            praiseVoList.add(praiseVo);
        }

        return ResponseEntity.ok(praiseVoList);
    }

    /**
     * 个人收藏笔记
     * @return
     */
    @GetMapping("/collection")
    public ResponseEntity userCollectionInfo(){
        //从线程当中获取用户id（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();

        //根据用户id查询收藏笔记列表
        List<Collection> collectionList = collectionService.findCollection(userId);
        //遍历praiseList获取noteId，并根据noteId查询笔记信息
        //接收查询出来的笔记信息
        List<CollectionVo> collectionVoList = new ArrayList<>();
        for (Collection c:collectionList
             ) {
            Long noteId = c.getNoteId();
            Note note = noteService.findNoteById(noteId);
            User user = userService.findUser(note.getId());

            CollectionVo collectionVo = new CollectionVo();
            collectionVo.setNote(note);
            collectionVo.setUser(user);

            collectionVoList.add(collectionVo);
        }

        return ResponseEntity.ok(collectionVoList);
    }

    /**
     * 提现
     * @return
     */
    @PutMapping("/withdraw")
    public ResponseEntity withdraw(){
        //从线程当中获取用户id（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();
        //查询用户的待返金额,并加入余额
        User user = userService.findUser(userId);
        //如果待返金额小于等于0或不存在
        if(user.getReturnMoney() == null || user.getReturnMoney() <= 0){
            return ResponseEntity.status(500).body("提现失败,待返金额不足");
        }
        userService.updateMoney(user.getReturnMoney(),userId);

        return ResponseEntity.status(200).body("提现成功");
    }
}
